package com.hzeport.bgs22emduty.xxljob;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.hzeport.bgs22emduty.config.SystemConfig;
import com.hzeport.bgs22emduty.dto.DutyPersonSubsidyStatusDTO;
import com.hzeport.bgs22emduty.entity.DutyScheduleInfo;
import com.hzeport.bgs22emduty.outbound.DutyScheduleInfoRepository;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class SubsidyXxlJob {

    @Resource
    private DutyScheduleInfoRepository dutyScheduleInfoRepository;

    @Resource
    private SystemConfig systemConfig;

    @XxlJob("getDutyPersonSubsidyStatus")
    public ReturnT<String> getDutyPersonSubsidyStatus(String param) {
        //查询当天值班人员
        List<DutyScheduleInfo> dutyScheduleInfos = dutyScheduleInfoRepository.list();
        dutyScheduleInfos.forEach(dutyScheduleInfo -> {
            RestTemplate restTemplate = new RestTemplate();
            HttpHeaders httpHeaders = new HttpHeaders();
            httpHeaders.add("Content-Type", "application/json");
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("beginDate",String.valueOf(dutyScheduleInfo.getDDay()));
            jsonObject.put("endDate",String.valueOf(dutyScheduleInfo.getDDay()));
            jsonObject.put("deptId",String.valueOf(dutyScheduleInfo.getDDept()));
            jsonObject.put("userName",String.valueOf(dutyScheduleInfo.getDUsername()));
            try {
                HttpEntity httpEntity = new HttpEntity(jsonObject, httpHeaders);
                ResponseEntity<String> result = restTemplate.postForEntity(systemConfig.getSubsidyUrl(),httpEntity, String.class);
                log.info("返回json:{}"+result.getBody());
                JSONObject resultJSON = JSONObject.parseObject(result.getBody());
                JSONArray jsonArray = resultJSON.getJSONArray("data");

                if (ObjectUtil.isNotEmpty(jsonArray)) {
                    List<DutyPersonSubsidyStatusDTO> dutyPersonSubsidyStatusDTOS = JSONArray.parseArray(JSONArray.toJSONString(jsonArray), DutyPersonSubsidyStatusDTO.class);
                    dutyPersonSubsidyStatusDTOS.forEach(dutyPersonSubsidyStatusDTO -> {
                        String time = "";
                        if (dutyPersonSubsidyStatusDTO.getOnDutyInfo().getBeginTime().equals("8:30")) {
                            if (dutyPersonSubsidyStatusDTO.getOnDutyInfo().getEndTime().equals("12:30")) {
                                time = "上午";
                            }
                            if (dutyPersonSubsidyStatusDTO.getOnDutyInfo().getEndTime().equals("17:30")) {
                                time = "白天";
                            }
                        }
                        if (dutyPersonSubsidyStatusDTO.getOnDutyInfo().getBeginTime().equals("12:30")) {
                            if (dutyPersonSubsidyStatusDTO.getOnDutyInfo().getEndTime().equals("17:30")) {
                                time = "下午";
                            }
                        }
                        if (dutyPersonSubsidyStatusDTO.getOnDutyInfo().getBeginTime().equals("17:30")) {
                            if (dutyPersonSubsidyStatusDTO.getOnDutyInfo().getEndTime().equals("8:30")) {
                                time = "夜间";
                            }
                        }
                        if (dutyScheduleInfo.getDutyTimesString().contains(time)) {
                            String isHasLeaveInLien;
                            if (dutyPersonSubsidyStatusDTO.getHasLeaveInLieu()) {
                                isHasLeaveInLien = "1";
                            }else {
                                isHasLeaveInLien = "0";
                            }
                            if (StringUtils.isBlank(dutyScheduleInfo.getIsDraw()) || !isHasLeaveInLien.equals(dutyScheduleInfo.getIsDraw())) {
                                LambdaUpdateWrapper<DutyScheduleInfo> wrapper = new LambdaUpdateWrapper<>();
                                wrapper.eq(DutyScheduleInfo::getId,dutyScheduleInfo.getId())
                                        .set(DutyScheduleInfo::getIsDraw,isHasLeaveInLien);
                                dutyScheduleInfoRepository.update(wrapper);
                            }
                        }
                    });
                }else {
                    if (StringUtils.isBlank(dutyScheduleInfo.getIsDraw())) {
                        LambdaUpdateWrapper<DutyScheduleInfo> wrapper = new LambdaUpdateWrapper<>();
                        wrapper.eq(DutyScheduleInfo::getId,dutyScheduleInfo.getId())
                                .set(DutyScheduleInfo::getIsDraw,"0");
                        dutyScheduleInfoRepository.update(wrapper);
                    }
                }
            } catch (Exception e) {
                log.error("远程请求更新补贴支取情况接口失败："+e);
            }
        });
        return ReturnT.SUCCESS;
    }

}
